package com.yonyou.crm.bpub.approval.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 根据约定
 * 1代表本天，
 * 2代表本周，
 * 3代表本季度，
 * 4代表本年，
 * 5代表不限
 * @author jiaoxw
 *
 */
public class ProcessQueryDateUtil {
	private final static Logger logger = LoggerFactory.getLogger(ProcessQueryDateUtil.class);
	
	/**
	 * i表示查询的时间范围，1代表本天，2代表本周，3代表本季度，4代表本年，5代表不限
	 * 
	 * @param i
	 * @return
	 */
	@SuppressWarnings("unused") 
	public static List<Date> getDate(int i) {
		List<Date> dates = new ArrayList<Date>();
		Date date = null;
		switch (i) {
		case 1:
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String todayStr = sdf.format(new Date()).substring(0, 10);
			try {
				Date today0 = sdf.parse(todayStr + " 00:00:00");
				Date today1 = sdf.parse(todayStr + " 23:59:59");
				dates.add(today0);
				dates.add(today1);
			} catch (ParseException e) {
				logger.error("时间解析错误", e);
			}
			break;
		case 2:
			dates.add(getWeekStartDate());
			dates.add(new Date());
			break;
		case 3:
			dates.add(getCurrentQuarterStartTime());
			dates.add(new Date());
			break;
		case 4:
			dates.add(getCurrentYearStartTime());
			dates.add(new Date());
			break;
		default:
			dates = null;
			break;
		}
		return dates;
	}
	
	/**
	 * 获取本周第一天
	 * @return
	 */
	public static Date getWeekStartDate(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Date date = cal.getTime();
		return date;
	}

	 /**
     * 当前季度的开始时间，即2018-01-16 00:00:00
     * 
     * @return
     */
    public static   Date getCurrentQuarterStartTime() {
    	SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前年的开始时间，即2018-01-16 00:00:00
     * 
     * @return
     */
    @SuppressWarnings("unused")
	public static   Date getCurrentYearStartTime() {
    	SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
}
